package com.wyzt.cart.service;

import com.wyzt.auth.entiy.UserInfo;
import com.wyzt.cart.interceptor.UserInterceptor;
import com.wyzt.cart.pojo.Cart;
import com.wyzt.common.enums.ExceptionEnum;
import com.wyzt.common.exception.WyztException;
import com.wyzt.common.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CartService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final String KEY_PREFIX = "cart:user:id";

    public void addCart(Cart cart) {
        //获取登陆用户
        UserInfo user = UserInterceptor.getUser();
        //key
        String key = KEY_PREFIX + user.getId();
        //hashKey
        String hashKey = cart.getSkuId().toString();
        //记录num
        Integer num = cart.getNum();

        BoundHashOperations<String, Object, Object> operations = redisTemplate.boundHashOps(key);

        //判断当前购物车商品是否存在
        if (operations.hasKey(hashKey)) {
            //是，修改数量
            String json = operations.get(hashKey).toString();
            cart = JsonUtils.parse(json, Cart.class);
            cart.setNum(cart.getNum() + num);
        }
        //写会redis
        operations.put(hashKey, JsonUtils.serialize(cart));

    }

    public List<Cart> queryCartList() {
        //获取登陆用户
        UserInfo user = UserInterceptor.getUser();
        //key
        String key = KEY_PREFIX + user.getId();

        if (!redisTemplate.hasKey(key)) {
            //key不存在，返回404
            throw new WyztException(ExceptionEnum.CART_NOT_FOUND);
        }

        //获取登录用户的所有购物车信息
        BoundHashOperations<String, Object, Object> operations = redisTemplate.boundHashOps(key);
        List<Cart> list = operations.values()
                .stream()
                .map(o -> JsonUtils.parse(o.toString(), Cart.class))
                .collect(Collectors.toList());
        return list;

    }

    public void updateCartNum(Long id, Integer num) {
        //获取登陆用户
        UserInfo user = UserInterceptor.getUser();
        //key
        String key = KEY_PREFIX + user.getId();
        //获取操作
        BoundHashOperations<String, Object, Object> operations = redisTemplate.boundHashOps(key);
        if (!redisTemplate.hasKey(id.toString())) {
            //key不存在，返回404
            throw new WyztException(ExceptionEnum.CART_NOT_FOUND);
        }
        //查询
        Cart cart = JsonUtils.parse(operations.get(id.toString()).toString(), Cart.class);
        cart.setNum(num);
        operations.put(id.toString(), JsonUtils.serialize(cart));

    }

    public void deleteCart(Long skuId) {
        //获取登陆用户
        UserInfo user = UserInterceptor.getUser();
        //key
        String key = KEY_PREFIX + user.getId();
        //删除
        redisTemplate.opsForHash().delete(key, skuId.toString());

    }
}
